package com.zks.example;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

public class AutomicExample {

    public static void main(String[] args) {
            Person person=new Person();
        AtomicIntegerFieldUpdater<Person> atomicIntegerFieldUpdater=
                AtomicIntegerFieldUpdater.newUpdater(Person.class,"age");
            for(int i=0;i<10;i++){
                Thread thread=new Thread(()->{
                    System.out.println(atomicIntegerFieldUpdater.getAndIncrement(person));
                });
                thread.start();
            }

        AtomicReferenceFieldUpdater atomicReferenceFieldUpdater=
                AtomicReferenceFieldUpdater.newUpdater(Person.class,String.class,"name");
        atomicReferenceFieldUpdater.compareAndSet(person,person.name,"dog");
        System.out.println(person.name);
    }

}

class Person{
    public volatile int age=1;

    public volatile String name="lisi";
}
